<?php
namespace Wap\Controller;
use Think\Controller;

class PaynotifyController extends Controller {
	
	public function notify(){
      file_put_contents('xinzhifu.txt',json_encode($_GET).PHP_EOL, FILE_APPEND);
      $sysconfig = M('sys_config')->find();
	  
		//bill_no	订单号
		//office_id  微信官方订单号
		//bill_fee	订单金额
		//pay_type 支付类型
		//link_id	透传参数（不填则不传）
		//sign		验签
		//feeResult	支付结果，0成功
		
		$bill_no = $_GET['bill_no'];
		$office_id = $_GET['office_id'];
		$bill_fee = $_GET['bill_fee'];
		$pay_type = $_GET['pay_type'];
		$link_id = $_GET['link_id'];
		$sign = $_GET['sign'];
		$feeResult = $_GET['feeResult'];
		$key = '1945c482d3dae61a32aac7c1979762cd';
		
		$mysign = strtoupper(md5("bill_no=".$bill_no."&bill_fee=".$bill_fee."&key=".$key));
		if($feeResult == 0 && $mysign == $sign){
		//这里写入数据库
			$danrow = M('user_chongzhi')->where(array('ddanhao' => $link_id, 'dcode' => 1, ))->find();
			if($danrow) {
				$userid = $danrow['userid']; //充值人id
				$djine = $bill_fee;  //充值金额
				
				//$user = M('user_list')->where(array('id' => $userid, ))->find();//充值人信息 //  $user = M('user_list')->where("id=$userid")->find();//充值人信息
				M()->execute("update __USER_ZHANGHU__ set uqianchong=uqianchong+$djine,uchongzong=uchongzong+$djine,cishu=cishu+1 where userid=$userid");
				M()->execute("update __USER_CHONGZHI__ set dcode=2,djisuan=2,djine=$djine where ddanhao='$link_id'");
				
				//20181109 Tim 充值次数送钱
			  $user = M('user_zhanghu')->where(array('userid' => $userid))->find();
			  
			  if ($user && $sysconfig && $sysconfig['chongnum'] > 0 && $sysconfig['songqian'] > 0 && $user['cishu'] >= $sysconfig['chongnum'])
			  {
					$song = $sysconfig['songqian'];
					M()->execute("update __USER_ZHANGHU__ set uqianchong=uqianchong+$song where userid=$userid");
					M()->execute("update __USER_ZHANGHU__ set cishu=0 where userid=$userid");
			  }
			  echo "SUCCESS";
			  exit();
			}else{
				M('sys_log')->add(array('lbiaoshi'=>'充值订单不存在','lcon'=>json_encode($_POST),'ltime'=>time()));
				echo 'FAIL';
				exit();
			}
		}else {
      	M('sys_log')->add(array('lbiaoshi'=>'充值失败','lcon'=>json_encode($_POST),'ltime'=>time()));
			echo 'FAIL';
			exit();
		}
		die;
}


public function notify2(){
      file_put_contents('fastpay.txt',json_encode($_POST));
      $sysconfig = M('sys_config')->find();
	  

		if (!function_exists('get_openid')) {

		require $_SERVER['DOCUMENT_ROOT'].'/fastpay/Fast_Cofig.php';

		}



		$sign=$_POST['sign_notify'];//获取签名

		$check_sign=notify_sign($_POST);

		if($sign!=$check_sign){
		  M('sys_log')->add(array('lbiaoshi'=>'充值失败','lcon'=>json_encode($_POST),'ltime'=>time()));
		  exit("签名失效");

		//签名计算请查看怎么计算签名,或者下载我们的SDK查看

		}



		$uid         = $_POST['uid'];//支付用户

		$total_fee   = $_POST['total_fee'];//支付金额

		$pay_title   = $_POST['pay_title'];//标题

		$sign        = $_POST['sign'];//签名

		$order_no    = $_POST['order_no'];//订单号

		$me_pri      = $_POST['me_pri'];//我们网站生成的金额,参与签名的,跟实际金额有差异

		
		//这里写入数据库
			$danrow = M('user_chongzhi')->where(array('ddanhao' => $order_no, 'dcode' => 1, ))->find();
			if($danrow) {
				$userid = $danrow['userid']; //充值人id
				$djine = $total_fee*100;  //充值金额
				
				//$user = M('user_list')->where(array('id' => $userid, ))->find();//充值人信息 //  $user = M('user_list')->where("id=$userid")->find();//充值人信息
				M()->execute("update __USER_ZHANGHU__ set uqianchong=uqianchong+$djine,uchongzong=uchongzong+$djine,cishu=cishu+1 where userid=$userid");
				M()->execute("update __USER_CHONGZHI__ set dcode=2,djisuan=2,djine=$djine where ddanhao='$order_no'");
				
				//20181109 Tim 充值次数送钱
			  $user = M('user_zhanghu')->where(array('userid' => $userid))->find();
			  
			  if ($user && $sysconfig && $sysconfig['chongnum'] > 0 && $sysconfig['songqian'] > 0 && $user['cishu'] >= $sysconfig['chongnum'])
			  {
					$song = $sysconfig['songqian'];
					M()->execute("update __USER_ZHANGHU__ set uqianchong=uqianchong+$song where userid=$userid");
					M()->execute("update __USER_ZHANGHU__ set cishu=0 where userid=$userid");
			  }
			  echo "SUCCESS";
			  exit();
			}else{
				M('sys_log')->add(array('lbiaoshi'=>'充值订单不存在','lcon'=>json_encode($_POST),'ltime'=>time()));
				echo 'FAIL';
				exit();
			}
		}
}
?>